﻿using System;
using System.Globalization;
using System.Web.UI.WebControls;
using Pantry.Business;
using System.Data;
using Pantry.Commons;

namespace Pantry.Web.Users
{
    public partial class ViewListOrganization : System.Web.UI.Page
    {
        private readonly IFormatProvider _formatProvider = new CultureInfo("en-gb");
        private OrganizationBus _orBus;
        private DataView _dataView;
        private static bool _isSort = true;

        protected void Page_Load(object sender, EventArgs e)
        {
            CollectionPager.DataSourcePaged.CurrentPageIndex = 1;
            Page.Title = "View list Organization";
            RemoveActiveOnButton();
            LinkGroupAll.CssClass = "button-active";
            FillOrganizationToGridview(CheckBoxIncludeActivate.Checked);
        }

        /// <summary>
        /// Fills the Organization to gridview.
        /// </summary>
        private void FillOrganizationToGridview(bool isInActived)
        {
            try
            {
                _orBus = new OrganizationBus();
                DataTable dataTable = _orBus.SelectAllOrganization();
                _dataView = dataTable.DefaultView;
                if(!isInActived)
                {
                    _dataView.RowFilter = "IsActive = 'Yes'";
                    _dataView.Sort = "Name ASC";
                }

                CollectionPager.PageSize = 15;
                CollectionPager.BindToControl = RepeaterListOrganization;
                CollectionPager.DataSource = _dataView;
                RepeaterListOrganization.DataSource = CollectionPager.DataSourcePaged;
                RepeaterListOrganization.DataBind();
            }
            catch (ApplicationException ex)
            {
                ExceptionHandler.WriteErrorLog(ex);
            }
        }

        /// <summary>
        /// Orders the Organization.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="isInActived">if set to <c>true</c> [is in actived].</param>
        private void OrderOrganization(string target, bool isInActived)
        {
            try
            {
                _orBus = new OrganizationBus();
                DataTable dataTable = _orBus.SelectAllOrganization();
                _dataView = dataTable.DefaultView;
                string condition = "";
                if (target.Length > -1)
                {
                    for (int i = 0; i < target.Length; i++)
                    {
                        if (i == target.Length - 1)
                        {
                            if (!isInActived)
                            {
                                condition += "Name LIKE '" + target.Substring(i - 1, 1) + "*') AND IsActive ='True'";
                            }
                            else
                            {
                                condition += "Name LIKE '" + target.Substring(i - 1, 1) + "*')";
                            }
                        }
                        else if (i == 0)
                        {
                            condition += "(";
                        }
                        else
                        {
                            condition += "Name LIKE '" + target.Substring(i - 1, 1) + "*' OR ";
                        }
                    }
                    _dataView.RowFilter = condition;
                }
                CollectionPager.PageSize = 15;
                CollectionPager.BindToControl = RepeaterListOrganization;
                CollectionPager.DataSource = _dataView;
                RepeaterListOrganization.DataSource = CollectionPager.DataSourcePaged;
                RepeaterListOrganization.DataBind();
            }
            catch (ApplicationException ex)
            {
                ExceptionHandler.WriteErrorLog(ex);
            }    
        }

		
        /// <summary>
        /// Shorts the Organization.
        /// </summary>
        /// <param name="field">The field.</param>
        private void SortOrganization(string field)
        {
            try
            {
                if (_isSort)
                {
                    _dataView.Sort = string.Format(_formatProvider, "{0} DESC", field);
                    _isSort = false;
                }
                else
                {
                    _dataView.Sort = string.Format(_formatProvider, "{0} ASC", field);
                    _isSort = true;
                }
            }
            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                throw;
            }
        }

        /// <summary>
        /// Sorts the name click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void SortNameClick(object sender, EventArgs e)
        {
            SortOrganization("Name");
        }

        /// <summary>
        /// Sorts the addr1 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void SortAddress1Click(object sender, EventArgs e)
        {
            SortOrganization("Address1");
        }

        /// <summary>
        /// Sorts the post code click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void SortPostCodeClick(object sender, EventArgs e)
        {
            SortOrganization("PostCode");
        }

        /// <summary>
        /// Sorts the contact click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void SortContactClick(object sender, EventArgs e)
        {
            SortOrganization("ContactName");
        }

        /// <summary>
        /// Sorts the is active click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void SortIsActiveClick(object sender, EventArgs e)
        {
            SortOrganization("IsActive");
        }

        /// <summary>
        /// Links the group all click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroupAllClick(object sender, EventArgs e)
        {
            FillOrganizationToGridview(CheckBoxIncludeActivate.Checked);
            RemoveActiveOnButton();
            LinkGroupAll.CssClass = "button-active";
        }
        /// <summary>
        /// Links the group0 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup0Click(object sender, EventArgs e)
        {
            OrderOrganization("0123456789", CheckBoxIncludeActivate.Checked);
            RemoveActiveOnButton();
            LinkGroup0.CssClass = "button-active";
        }
        /// <summary>
        /// Links the group1 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup1Click(object sender, EventArgs e)
        {
            OrderOrganization("ABCDE", CheckBoxIncludeActivate.Checked);
            RemoveActiveOnButton();
            LinkGroup1.CssClass = "button-active";
        }
        /// <summary>
        /// Links the group2 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup2Click(object sender, EventArgs e)
        {
            OrderOrganization("FGHIJ", CheckBoxIncludeActivate.Checked);
            RemoveActiveOnButton();
            LinkGroup2.CssClass = "button-active";
        }
        /// <summary>
        /// Links the group3 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup3Click(object sender, EventArgs e)
        {
            OrderOrganization("KLMN", CheckBoxIncludeActivate.Checked);
            RemoveActiveOnButton();
            LinkGroup3.CssClass = "button-active";
        }
        /// <summary>
        /// Links the group4 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup4Click(object sender, EventArgs e)
        {
            OrderOrganization("OPQR", CheckBoxIncludeActivate.Checked);
            RemoveActiveOnButton();
            LinkGroup4.CssClass = "button-active";
        }
        /// <summary>
        /// Links the group5 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup5Click(object sender, EventArgs e)
        {
            OrderOrganization("STUV", CheckBoxIncludeActivate.Checked);
            RemoveActiveOnButton();
            LinkGroup5.CssClass = "button-active";
        }
        /// <summary>
        /// Links the group6 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup6Click(object sender, EventArgs e)
        {
            OrderOrganization("WXYZ", CheckBoxIncludeActivate.Checked);
            RemoveActiveOnButton();
            LinkGroup6.CssClass = "button-active";
        }

        public void CreateSupportClick(object sender, EventArgs e)
        {
            Response.Redirect("OrganizationDetails.aspx?action=create");
        }
        protected void MarkActivated(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
        {
            var hId = (HiddenField)RepeaterListOrganization.Items[e.Item.ItemIndex].FindControl("HiddenFieldOrganizationId");
            var values = hId.Value;
            if (e.CommandName == "update")
            {
                var id = int.Parse(values);
                _orBus = new OrganizationBus();
                var result = _orBus.MarkActivated(true,id);
                FillOrganizationToGridview(CheckBoxIncludeActivate.Checked);
                Response.Redirect("OrganizationDetails.aspx?action=update&id=" + values + "");
            }
            else
            {
                Response.Redirect("OrganizationDetails.aspx?action=update&id=" + values + "");
            }
        }
        protected void CheckboxIsActiveChanged(object sender, EventArgs e)
        {
            FillOrganizationToGridview(CheckBoxIncludeActivate.Checked);
        }
        protected void RemoveActiveOnButton()
        {
            LinkGroupAll.CssClass = "button-in-active";
            LinkGroup6.CssClass = "button-in-active";
            LinkGroup5.CssClass = "button-in-active";
            LinkGroup4.CssClass = "button-in-active";
            LinkGroup3.CssClass = "button-in-active";
            LinkGroup2.CssClass = "button-in-active";
            LinkGroup1.CssClass = "button-in-active";
            LinkGroup0.CssClass = "button-in-active";
        }
    }
}
